var app = getApp();
var count = 0; //开启车辆的数量
Page({
    data: {
        items: [],
        startX: 0, //开始坐标
        startY: 0,
        AllVehicles: [] //物流公司车辆列表
    },
    onShow: function() {
        // for (var i = 0; i < this.data.AllVehicles.length; i++) {
        //     this.data.AllVehicles.push({
        //         content: i + " 向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦",
        //         isTouchMove: false //默认全隐藏删除
        //     })
        // }
        // this.setData({
        //     items: this.data.items
        // })
        wx.showLoading({
            title: '加载中',
        })
        var that = this

        app.getApi.request("/api/Vehicles/MyVehicles", { id: app.getApi.drivers.id, name: app.getApi.drivers.name, tel: app.getApi.drivers.tel }, "POST", (res) => {
            if (res.data.responseCode == 1000) {
                wx.hideLoading()
                app.getApi.drivers.vehicles_DriversList = res.data.obj
                for (let i = 0; i < res.data.obj.length; i++) {
                    res.data.obj[i].isTouchMove = false
                }

                that.setData({
                    AllVehicles: res.data.obj
                })
                for (let i = 0; i < that.data.AllVehicles.length; i++) {
                    if (that.data.AllVehicles[i].isVehicleUsing) {
                        count++
                    }
                }
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'loading',
                    duration: 2000
                });
            }
        }, (res) => {})
    },
    switch2Change(e) {
        var that = this;
        var AllVehicles = that.data.AllVehicles
        if (!e.target.dataset.using) {
            console.log("开启车辆")
            if (count == 1) {
                app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].isVehicleUsing = false
                that.setData({
                    AllVehicles: AllVehicles
                })
                wx.showModal({
                    title: '提示',
                    content: "同时只能开启一辆车",
                })

                return;
            }
            count++
            app.getApi.request("/api/Vehicles/UsingVehicle", { vehicles: { id: e.target.dataset.id, platenumber: e.target.dataset.platenumber }, drivers: { id: app.getApi.drivers.id, name: app.getApi.drivers.name, tel: app.getApi.drivers.tel } }, "POST", (res) => {
                if (res.data.responseCode == 1000) {
                    console.log(app.getApi.drivers.vehicles_DriversList, e)
                    console.log(app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index])
                    console.log(app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].isVehicleUsing)
                    AllVehicles[e.currentTarget.dataset.index].isVehicleUsing = true
                    app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].isVehicleUsing = true
                    that.setData({
                        AllVehicles: AllVehicles
                    })

                    // app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].vehicles = { //赋值公共属性，保存车辆信息
                    //     id: e.target.dataset.id,
                    //     platenumber: e.currentTarget.dataset.platenumber
                    // }
                    console.log(that.data.AllVehicles)
                } else {
                    wx.showToast({
                        title: '启用车辆失败',
                        icon: 'loading',
                        duration: 2000
                    });

                }
            }, (res) => {})


        } else {
            console.log("关闭车辆")
            if (count == 1) { //有开启的车辆，关闭是重新复制开启车辆的数量为0；
                count = 0
            }
            app.getApi.request("/api/Vehicles/DisableUsingVehicle", { vehicles: { id: e.target.dataset.id, platenumber: e.target.dataset.platenumber }, drivers: { id: app.getApi.drivers.id, name: app.getApi.drivers.name, tel: app.getApi.drivers.tel } }, "POST", (res) => {
                if (res.data.responseCode == 1000) {
                    console.log(app.getApi.drivers.vehicles_DriversList, e)
                    console.log(app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index])
                    console.log(app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].isVehicleUsing)
                    app.getApi.drivers.vehicles_DriversList[e.currentTarget.dataset.index].isVehicleUsing = false //停止车辆，公共属性车辆信息清空
                    AllVehicles[e.currentTarget.dataset.index].isVehicleUsing = false
                    that.setData({
                        AllVehicles: AllVehicles
                    })
                    console.log(that.data.AllVehicles)
                        // this.setData({
                        //     AllVehicles: res.data.obj
                        // })
                } else {
                    wx.showToast({
                        title: '停用车辆失败',
                        icon: 'loading',
                        duration: 2000
                    });
                }
            }, (res) => {})
        }

    },
    addCart() {
        wx.navigateTo({
            url: '../addCart/addCart'
        })
    },
    cartDetail(e) {
        wx.navigateTo({
            url: '../cartDetail/cartDetail?id=' + e.currentTarget.dataset.id
        })
    },
    //手指触摸动作开始 记录起点X坐标
    touchstart: function(e) {
        console.log(e)
            //开始触摸时 重置所有删除
        this.data.AllVehicles.forEach(function(v, i) {
            if (v.isTouchMove) //只操作为true的
                v.isTouchMove = false;
        })

        this.setData({
            startX: e.changedTouches[0].clientX,
            startY: e.changedTouches[0].clientY,
            AllVehicles: this.data.AllVehicles
        })
        console.log(this.data.AllVehicles)
    },
    //滑动事件处理
    touchmove: function(e) {
        console.log(e)
        var that = this,
            index = e.currentTarget.dataset.index, //当前索引
            startX = that.data.startX, //开始X坐标
            startY = that.data.startY, //开始Y坐标
            touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
            touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
            //获取滑动角度
            angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
        that.data.AllVehicles.forEach(function(v, i) {
                v.isTouchMove = false
                    //滑动超过30度角 return
                if (Math.abs(angle) > 30) return;
                if (i == index) {
                    if (touchMoveX > startX) //右滑
                        v.isTouchMove = false
                    else //左滑
                        v.isTouchMove = true
                }
            })
            //更新数据
        that.setData({
            AllVehicles: that.data.AllVehicles
        })
    },
    /**
     * 计算滑动角度
     * @param {Object} start 起点坐标
     * @param {Object} end 终点坐标
     */
    angle: function(start, end) {
        var _X = end.X - start.X,
            _Y = end.Y - start.Y
            //返回角度 /Math.atan()返回数字的反正切值
        return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    //删除事件
    // del: function(e) {
    //     this.data.AllVehicles.splice(e.currentTarget.dataset.index, 1)
    //     this.setData({
    //         AllVehicles: this.data.AllVehicles
    //     })
    // }
})